package cn.icare.cms.impl;

import cn.icare.cms.ItemPicService;
import cn.icare.cms.dao.mapper.ItemPicMapper;
import cn.icare.cms.dao.po.ItemCat;
import cn.icare.cms.dao.po.ItemPic;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemPicServiceImpl implements ItemPicService {

    @Resource
    private ItemPicMapper itemPicMapper;

    @Override
    public boolean SavePicList(Long itemid, List<String> pics, Boolean isEdit) {
        if(isEdit)
        {
            //先删除原数据
           Example example = new Example(ItemPic.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("itemid",itemid);
            this.itemPicMapper.deleteByExample(example);
        }
        if(pics!=null &&pics.size()>0)
        {
            ItemPic itemPic;
            for(String pic:pics)
            {
                itemPic=new ItemPic();
                itemPic.setItemid(itemid);
                itemPic.setImageurl(pic);
                this.itemPicMapper.insertSelective(itemPic);
            }
        }
        return false;
    }

    @Override
    public List<String> getPicList(Long itemid) {
        List<String> picList=new ArrayList<>();
        Example example = new Example(ItemPic.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemid",itemid);
       List<ItemPic> itemPics= this.itemPicMapper.selectByExample(example);
       if(itemPics!=null && itemPics.size()>0)
       {
           for (ItemPic pic:itemPics)
           {
               picList.add(pic.getImageurl());
           }
       }
       return picList;

    }
}
